<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改密码</title>
    <%@ include file="/WEB-INF/pages/commons/head-meta.jsp" %>
    <%@ include file="/WEB-INF/pages/commons/js-meta.jsp" %>
</head>
<style>
    .font {
        font-size: 18px;
        width: 100px;
        float: left;
        padding-top: 3px;

    }

    .text {
        height: 40px;
        width: 400px;
        border-color: #D8D8D8;
        border-width: 1px;
        float: left;
        text-indent: 5px;
    }

    .input-group {
        margin-top: 40px;
    }

    .msgOld, .msgNew, .msgReg {
        float: left;
        color: red;
        font-size: 16px;
        margin-left: 25px;
        padding-top: 4px;
    }
</style>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>修改密码</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link" onclick="reset()"> <i
                                class="glyphicon glyphicon-refresh"></i>
                        </a>
                    </div>
                </div>
                <!-- 创建账户  -->
                <div class="ibox-content">
                    <div class="input-group">
                        <div class="font">旧密码：</div>
                        <input type="password" placeholder="请输入旧密码" name="oldPwd" class="text"
                               onfocus="this.placeholder=''"/><span class="msgOld"></span>
                    </div>
                    <div class="input-group">
                        <span class="font">新密码：</span>
                        <input type="password" placeholder="请输入新密码" name="password" class="text"
                               onfocus="this.placeholder=''"/><span class="msgNew"></span>
                    </div>
                    <div class="input-group">
                        <span class="font">确认密码：</span>
                        <input type="password" placeholder="请输入确认密码" name="regPwd" class="text"
                               onfocus="this.placeholder=''"/><span class="msgReg"></span>
                    </div>
                    <div class="input-group">
                        <button class="btn btn-primary" type="button" onclick="save()">保存</button>
                        <button class="btn btn-primary" type="button" style="margin-left: 390px;">取消</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    function clearMsg() {
        $(".msgNew").html("");
        $(".msgOld").html("");
        $(".msgReg").html("");
    }

    function save() {
        var pwd = $("input[name='password']").val();
        var oldPwd = $("input[name='oldPwd']").val();
        var regPwd = $("input[name='regPwd']").val();
        var pwdReg = /[\w]{6,100}/;
        clearMsg();
        if (oldPwd == null || oldPwd == '') {
            $(".msgOld").html("请输入旧密码");
            return;
        }
        if (pwd == null || pwd == '') {
            $(".msgNew").html("请输入新密码");
            return;
        }
        if (!pwdReg.test(pwd)) {
            $(".msgNew").html("不小于6位的字母或数字密码");
            return;
        }
        if (regPwd == null || regPwd == '') {
            $(".msgReg").html("请输入确认密码");
            return;
        }
        if (regPwd !== pwd) {
            $(".msgReg").html("密码不一致");
            return;
        }
        axios.post(ctx + '/admin/admin/get', {oldPwd: oldPwd})
            .then(function (res) {
                if (!res.data.success) {
                    $(".msgOld").html(res.data.info);
                    return;
                }
                axios.post(ctx + '/admin/admin/modify-pwd', {pwd: pwd})
                    .then(function (res) {
                        if (!res.data.success) {
                            layer.msg(res.data.info, {icon: 2});
                            return;
                        }
                        layer.msg("密码修改成功", {icon: 6});
                        setTimeout(function () {
                            location.href = ctx + "/admin/login";
                        }, 1000);
                    })
            })
            .catch(function (res) {
                layer.msg("网络异常", {icon: 5});
            })
    }

    function  reset() {
        window.location.reload();
    }
</script>
</html>
